<?php
session_start();
require_once '../kelas/kategori.php';

require_once '../kelas/produk.php';
require_once '../kelas/tr_barang_masuk.php';

$fungsi = new fungsi();
$objProduk = new Produk();
$keamanan = new amankanParam();
$dtGet = $keamanan->getMethod();
$dtPost = $keamanan->postMethod();
if (isset($dtGet['on']) && strlen($dtGet['on']) > 0) {
    $on = $dtGet['on'];
    switch ($on) {
        case 'simpandetail':
            $reset = isset($dtPost['reset']) ? $dtPost['reset'] : NULL;
            $svDet = $objTransBarangMasuk->addDetail($dtPost['idproduk'], $dtPost['jumlah'], $reset);
            echo json_encode($svDet);
            break;
        case 'cekBarang':
            $cekDet = $objTransBarangMasuk->cekSesDetail($dtPost['idproduk']);
            echo json_encode($cekDet);
            break;
        case 'simpan':
            $objTransBarangMasuk->namaTabel = 'barang_masuk';
            $notr = $objTransBarangMasuk->createTransNum();
            if ($notr['success'] == false) {
                echo json_encode($notr);
                exit;
            } else {
                //simpan Final
                $dtSv = $dtPost;
                $dtSv['no_tr'] = $notr['result'];
                $dtSv['date_bm'] = date('Y-m-d', strtotime($dtSv['date_bm']));
                $dtSv['waktu'] = date('Y-m-d H:i:s');
                $dtSv['iduser'] = $_SESSION['adminlogin']['id'];
                $objTransBarangMasuk->namaTabel = 'barang_masuk';
                $objTransBarangMasuk->create($dtSv);
                $objTransBarangMasuk->execute();
                $lastAIncID = $objTransBarangMasuk->getLastIdUseArr($dtSv);
                $dtSesBM = $objTransBarangMasuk->detailTemp();
                $objTransBarangMasuk->namaTabel = 'stok';
                $suc = true;
                foreach ($dtSesBM['result'] as $dtDetail) {
                    $dtDetail['idbm']=$lastAIncID;
                    unset($dtDetail['nama']);
                    $objTransBarangMasuk->create($dtDetail);
                    $exc=$objTransBarangMasuk->execute();
                    if($exc['success']==false){
                        $suc=false;
                    }
                };
                if($suc==true){
                    unset($_SESSION['detail_barang_masuk']);
                }
            }
            echo json_encode($exc);
            break;
        case 'hapusdetail':
            $dtSes = $objTransBarangMasuk->detailTemp();
            foreach ($dtSes['result'] as $key => $value) {
                if ($value['idproduk'] == $dtPost['idproduk']) {
                    $keyDel = $key;
                }
            };
            if (isset($keyDel) && isset($_SESSION['detail_barang_masuk']) && isset($_SESSION['detail_barang_masuk'][$keyDel])) {
                unset($_SESSION['detail_barang_masuk'][$keyDel]);
            };
            $dtReOrd = $objTransBarangMasuk->detailTemp();
            if (isset($_SESSION['detail_barang_masuk'])) {
                unset($_SESSION['detail_barang_masuk']);
            }
            foreach ($dtReOrd['result'] as $vReOrd) {
                $_SESSION['detail_barang_masuk'][] = $vReOrd;
            };
            echo json_encode(array('success' => true));
            break;
        default:
            echo json_encode(array('success' => false, 'error_msg' => 'Not Set'));
            break;
    }
} else {
    echo json_encode(array('success' => false, 'error_msg' => 'Not Allowed For Direct Access'));
};